package com.yanqu.road.dao.impl.cross.cricket;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.cricket.court.CricketCourtGroupData;
import com.yanqu.road.entity.cricket.court.CricketCourtUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossCricketCourtGroupDaoImpl extends TempDataObjectDao<CricketCourtGroupData> {

    public Map<Integer, Map<Integer, CricketCourtGroupData>> getCricketCourtGroupDataMap(int round) {
        Map<Integer, Map<Integer, CricketCourtGroupData>> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String selectSql = "select * from t_c_cricket_court_group_data where round = ?";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            int i = 1;
            call = getDb().getConnection().prepareCall(selectSql);
            call.setInt(i++, round);
            rs = call.executeQuery();
            while (rs.next()) {
                CricketCourtGroupData data = parse(rs);
                Map<Integer, CricketCourtGroupData> groupMap = map.getOrDefault(data.getCourtId(), new ConcurrentHashMap<>());
                groupMap.put(data.getGroupId(), data);
                map.put(data.getCourtId(), groupMap);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    private static String insertSql = "insert into `t_c_cricket_court_group_data`(round,courtId,groupId,matchGroupId,num,updateTime) values (?,?,?,?,?,?)";

    @Override
    protected boolean addData(CricketCourtGroupData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> dbParameterMap = new HashMap<>();
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getRound()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getCourtId()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getMatchGroupId()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getNum()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getUpdateTime()));
        if (getDb().executeNoneQuery(insertSql, dbParameterMap) > 0) {
            return true;
        }
        return false;
    }

    private static String updateSql = "update `t_c_cricket_court_group_data` set `matchGroupId`=?,`num`=?,`updateTime`=? where round=? and courtId=? and groupId=?";

    @Override
    protected boolean updateData(CricketCourtGroupData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> dbParameterMap = new HashMap<>();
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getMatchGroupId()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getNum()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getUpdateTime()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getRound()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getCourtId()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        if (getDb().executeNoneQuery(updateSql, dbParameterMap) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected CricketCourtGroupData parseData(ResultSet rs) throws SQLException {
        CricketCourtGroupData data = new CricketCourtGroupData();
        data.setRound(rs.getInt("round"));
        data.setCourtId(rs.getInt("courtId"));
        data.setGroupId(rs.getInt("groupId"));
        data.setMatchGroupId(rs.getInt("matchGroupId"));
        data.setNum(rs.getInt("num"));
        data.setUpdateTime(rs.getLong("updateTime"));
        return data;
    }
}
